var num
var app= getApp();
import http from "../../../class/Utils/Http"
import tips from "../../../class/Utils/Tips"
Page({

  data: {
    luyinstate: '0',
    luyinplaytig: true,
    playRecord: 'playRecord',
    is_show: true,
    tempFilePathstig: 0,
    img_arr:[],
      uplimg:[],
      mp3_s:0,
      tongbu:0,
      btn_title:"加载中...",
      btn_dis:true,
      i:0,
      pic_post_data: []
  },

  onLoad: function (options) {
    var that = this;
    //牛逼的扫描名片功能 begin
    if (options.pic_post_data == 1) {

      var pic_post_data = wx.getStorageSync('pic_post_data')
      that.setData({
        pic_post_data: pic_post_data
      })
    }
    //牛逼的扫描名片功能 end

      //裁切图片用 begin
      let { avatar } = options
      if (avatar) {
          that.setData({
              tempFilePathstig: 1,
              uplimg: avatar
          })
      }
      //裁切图片用 end

    that.recorderManager = wx.getRecorderManager();
    that.recorderManager.onError(function () {
      that.tip("录音失败！")
      that.luyinshibai("录音失败执行动作")
    });
    that.recorderManager.onStop(function (res) {
        console.log(res)
      that.setData({
        src: res.tempFilePath,
          mp3_s:1
      })
      that.luyinwancheng("录音停止执行动作")
      //console.log(res.tempFilePath)
      that.tip("录音完成！请点击 '保存名片信息' 进行录音保存")
    });

    that.innerAudioContext = wx.createInnerAudioContext();
    that.innerAudioContext.onEnded((res) => {
      that.setData({
        playRecord: 'playRecord'
      })
    })
    that.innerAudioContext.onError((res) => {
      that.tip("播放录音失败！")
    })
      /*
      判断用户 首次进入  录入 userid
       */
      var user = wx.getStorageSync('user');
      //console.log(user)
      var s = options.s||0;
      var id = options.id||0;
      if(s == '1'){
          that.setData({
              mp_id:id
          })
          http.req("/Mingpian/panduan_qiye_mingpian",{userid:user.userid},function (resss) {
                if(resss.code == '1'){
                    wx.showModal({
                        title: '温馨提示',
                        content: '系统检测到之前设置过名片信息,是否同步',
                        success: function(res) {
                            if (res.confirm) {
                                http.req("/Mingpian/tongbu",{id:resss.id},function (infores) {
                                    //console.log(infores)
                                    that.setData({
                                        info:infores,
                                        tongbu:1
                                    })
                                    if(infores.qianmingmp3){
                                        that.setData({
                                            luyinstate: '0',
                                            primarytig: 'primary',
                                            luyinplaytig: false,
                                            src:infores.qianmingmp3
                                        })
                                    }
                                })
                            } else if (res.cancel) {
                                //console.log('用户点击取消')
                            }
                        }
                    })
                }
          })
      }else{
          http.req("/Mingpian/panduan",{userid:user.userid},function (res) {
              //console.log(res)
              that.setData({
                  mp_id:res
              })
              /*
               info
               */
              that.info()

          })
      }




  },
    info:function () {
      var that = this
        var user = wx.getStorageSync('user');
        http.reqload("/Mingpian/info",{userid:user.userid},function (infores) {
            console.log(123)
            console.log(infores)
            that.setData({
                info:infores,
                content_pic_list:infores.content_pic,
                btn_title:"保存名片信息",
                btn_dis:false
            })
            if(infores.qianmingmp3){
                that.setData({
                    luyinstate: '0',
                    primarytig: 'primary',
                    luyinplaytig: false,
                    src:infores.qianmingmp3
                })
            }

            // if(infores.content_pic.length>0){
            //         var imgarr = that.data.img_arr
            //         for(var i=0;i<infores.content_pic.length;i++){
            //             imgarr.push(infores.content_pic[i].pic)
            //         }
            //     that.setData({
            //         img_arr:imgarr
            //     })
            // }
        })
    },
  onShow: function () {
     var that = this
     if (that.data.src == null){
       that.setData({
         primarytig: 'default',
         luyinplaytig: 'false'
       })
     }else{
       that.setData({
         primarytig: 'primary',
         luyinplaytig: 'true'
       })
     }
    var pic_post_data = wx.getStorageSync('pic_post_data')
    that.setData({
      pic_post_data: pic_post_data
    })
  },
  //录音失败
  luyinshibai:function(shibai){
    var that = this
    that.setData({
      luyinstate: '0',
    })
  },
  //录音停止
  luyinwancheng:function(){
    var that = this
    that.setData({
      luyinstate: '0',
      primarytig: 'primary',
      luyinplaytig: false,
    })
  },
  /**生命周期函数--监听页面隐藏
  onHide: function () {
    var that= this
    var src = that.data.src;
    that.innerAudioContext.src = that.data.src;
    that.innerAudioContext.stop()
    
  },**/
  /**提示**/
  tip: function (msg) {
    wx.showModal({
      title: '提示',
      content: msg,
      showCancel: false
    })
  },
  /**录制mp3音频**/
   startRecordMp3: function () {
    var that = this
    that.recorderManager.start({
      format: 'mp3',
      duration: 15000,
    });
    that.setData({
      luyinstate: '1',
      primarytig: 'default',
      luyinplaytig: true,
      playRecord: 'playRecord',

    })
  },
  /**停止录音**/
   stopRecord: function () {
    var that = this
    that.recorderManager.stop()
    that.setData({
      luyinstate: '0',
      primarytig: 'primary',
      luyinplaytig: false,
    })
    var src = that.data.src;
    //console.log(src)
  },
  /**播放录音**/
  playRecord: function () {
    var that = this;
    var src = that.data.src;
      //console.log(src)
    if (src == '') {
      that.tip("请先录音！")
      return;
    }
    that.innerAudioContext.src = src;
    that.innerAudioContext.play()
    that.setData({
      playRecord: 'playRecordstop'
    })
    //console.log('这是播放事件' + that.data.playRecord)

  },
  //
  playRecordstop: function() {
    var that = this;
    var src = that.data.src;
    that.innerAudioContext.src = that.data.src;
    that.innerAudioContext.stop()
    that.setData({
      playRecord: 'playRecord'
    })
    //console.log('这是停止事件' + that.data.playRecord)
  },
  //删除录音
  deleteRecord:function(){
    var that=this
    // that.innerAudioContext.destroy()
    var src = that.data.src;

    wx.showModal({
      title: '删除提示',
      content: '确认要删除已有语音签名吗？删除后要重新录制',
      success: function (res) {
        if (res.confirm) {
            http.req("/Mingpian/del_luyin",{id:that.data.mp_id})
          that.innerAudioContext.stop()
          that.setData({
            src: '',
            luyinstate: '0',
            primarytig: 'default',
            luyinplaytig: 'false',
              mp3_s:0
          })
        } else if (res.cancel) {

        }
      }
    })



  },
  //选择图片发送
  uploadimg: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 选择图片的数量，最多9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有

      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片


          const src = res.tempFilePaths[0]
          wx.redirectTo({
              url: `../upload/upload?src=${src}&id=${that.data.mp_id}`
          })



        // var tempFilePaths = res.tempFilePaths
        // that.setData({
        //   tempFilePathstig: 1,
        //   uplimg: tempFilePaths
        // })
        //   var data = {
        //       id: that.data.mp_id
        //   }
        //   var rootDocment = app.httpurl.url
        //   wx.uploadFile({
        //       url: rootDocment + '/Mingpian/edit_mingpian',
        //       filePath: that.data.uplimg[0],
        //       name: 'pic', //文件对应的参数名字(key)
        //       formData: data,  //其它的表单信息
        //       success: function (suc) {
        //           //console.log(suc)
        //           var data = JSON.parse(suc.data);
        //           if (data.status == '1') {
        //               console.log("头像上传成功");
        //               tips.ok("头像上传成功")
        //
        //           }
        //       },
        //   })
      }
    })
  },
  upimg: function () {
    var that = this;
    var img_length = that.data.img_arr.length;
      http.req("/Mingpian/pic_num",{id:that.data.info.id},function (res) {
          console.log(res)
          var content_pic_length =res
          console.log(content_pic_length)
          wx.chooseImage({
              sizeType: ['compressed'],
              success: function (res) {
                  num = res.tempFilePaths.length + img_length+content_pic_length
                  if (num > 6) {
                      wx.showModal({
                          title: '温馨提示',
                          content: '最多上传6张图片',
                          showCancel:false
                      })
                      return false
                  }
                  that.setData({
                      img_arr: that.data.img_arr.concat(res.tempFilePaths),

                  })
                  if(that.data.info.qianmingmp3){
                      that.setData({
                          luyinstate: '0',
                          primarytig: 'primary',
                          luyinplaytig: false,
                      })
                  }

              }
          })
      })




  },
  xxx: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var img_arr = that.data.img_arr;
    img_arr.splice(index, 1);
    this.setData({
      img_arr: img_arr
    });
  },
  formSubmit: function (e) {

    var that = this;
    var user = wx.getStorageSync('user');
    that.setData({
      btn_title: "数据处理中。。",
      btn_dis: true
    })
    /**新加头像*/
    if (that.data.uplimg.length) {
      wx.uploadFile({
        url: app.httpurl.url + '/Api/change_touxiang',
        filePath: that.data.uplimg,
        name: 'pic', //文件对应的参数名字(key)
        formData: { uid: user.userid },  //其它的表单信息
        success: function (suc) {

          var formdata = {
            id: that.data.mp_id,
            xingming: e.detail.value.xingming,
            zhiwei: e.detail.value.zhiwei,
            gongsi: e.detail.value.gongsi,
            shouji: e.detail.value.shouji,
            guhua: e.detail.value.guhua,
            weixin: e.detail.value.weixin,
            youxiang: e.detail.value.youxiang,
            dizhi: e.detail.value.dizhi,
            qianming: e.detail.value.qianming,
          }
          http.req("/Mingpian/edit_mingpian", formdata, function (res) {
            if (that.data.img_arr.length > 0) {
              console.log("处理没头像多图");
              that.up(that.data.mp_id, function (res) {
                wx.hideLoading()
                tips.back()
              })
            } else {
              if (that.data.mp3_s == '1') {
                var mp3data = {
                  id: that.data.mp_id
                }
                var rootDocment = app.httpurl.url
                wx.uploadFile({
                  url: rootDocment + '/Mingpian/edit_mingpian',
                  filePath: that.data.src,
                  name: 'qianmingmp3', //文件对应的参数名字(key)
                  formData: mp3data,  //其它的表单信息
                  success: function (suc) {
                    //console.log(suc)
                    wx.hideLoading()
                    tips.back()
                  },
                })
              } else {
                wx.hideLoading()
                tips.back()
              }

            }
          })


        },
      })
    } else {
      var formdata = {
        id: that.data.mp_id,
        xingming: e.detail.value.xingming,
        zhiwei: e.detail.value.zhiwei,
        gongsi: e.detail.value.gongsi,
        shouji: e.detail.value.shouji,
        guhua: e.detail.value.guhua,
        weixin: e.detail.value.weixin,
        youxiang: e.detail.value.youxiang,
        dizhi: e.detail.value.dizhi,
        qianming: e.detail.value.qianming,
      }
      http.req("/Mingpian/edit_mingpian", formdata, function (res) {
        if (that.data.img_arr.length > 0) {
          console.log("处理没头像多图");
          that.up(that.data.mp_id, function (res) {
            wx.hideLoading()
            tips.back()
          })
        } else {
          if (that.data.mp3_s == '1') {
            var mp3data = {
              id: that.data.mp_id
            }
            var rootDocment = app.httpurl.url
            wx.uploadFile({
              url: rootDocment + '/Mingpian/edit_mingpian',
              filePath: that.data.src,
              name: 'qianmingmp3', //文件对应的参数名字(key)
              formData: mp3data,  //其它的表单信息
              success: function (suc) {
                //console.log(suc)
                wx.hideLoading()
                tips.back()
              },
            })
          } else {
            wx.hideLoading()
            tips.back()
          }

        }
      })
    }
  },

    up: function (id,cb) {
        var that = this;
        var data = {
            pid: id
        }
        var i = that.data.i
        console.log(that.data.img_arr)
        console.log(i)
        var rootDocment = app.httpurl.url
        wx.uploadFile({
            url: rootDocment + '/Mingpian/content_img',
            filePath: that.data.img_arr[i],
            name: 'pic', //文件对应的参数名字(key)
            formData: data,  //其它的表单信息
            success: function (suc) {
            }, complete: function (complete) {
                console.log("处理多图第"+i+"张");
                if(complete.data== '1'){
                    i++
                    if (i < that.data.img_arr.length) {//若图片还没有传完，则继续调用函数
                        that.setData({
                            i:i
                        })
                        that.up(id,cb)
                    }else{
                        console.log("多图处理完毕");
                        return typeof cb == "function" && cb()
                    }
                }

            }
        })
    },
    /*
    删除已有图片
     */
    content_pic_del:function (e) {
        var that = this
        var index = e.currentTarget.dataset.index
        wx.showModal({
            title: '温馨提示',
            content: '是否删除?',
            success: function(res) {
                if (res.confirm) {
                    var content_pic_list = that.data.content_pic_list;
                    http.req("/Mingpian/del_content_pic",{id:content_pic_list[index].id})
                    content_pic_list.splice(index, 1);
                    that.setData({
                        content_pic_list: content_pic_list
                    });
                } else if (res.cancel) {
                    //console.log('用户点击取消')
                }
            }
        })
    },
})